package com.snail.billing.json;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.snail.billing.DataCache;

public class JsonState {

	private String[] indexs;
	private Map<String, Platform> platforms;

	public JsonState(String jsonString) {
		DataCache cache = DataCache.getInstance();
		try {
			JSONObject json = new JSONObject(jsonString);
			JSONObject jsonGame = json.getJSONObject("game");
			JSONObject jsonPlatforms = json.getJSONObject("platforms");
			if (jsonGame.has(cache.importParams.gameId)) {
				indexs = jsonGame.getString(cache.importParams.gameId).split(",");
			}
			if (indexs == null) {
				return;
			}
			platforms = new HashMap<String, Platform>();
			for (String index : indexs) {
				if (jsonPlatforms.has(index)) {
					platforms.put(index, new Platform(jsonPlatforms.getString(index)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public String[] getIndexs() {
		return indexs;
	}

	public Map<String, Platform> getPlatforms() {
		return platforms;
	}

	public class Platform {
		private int platformId;
		private String name;

		public Platform(String jsonString) {
			try {
				JSONObject json = new JSONObject(jsonString);
				platformId = json.getInt("platformId");
				name = json.getString("name");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		public int getPlatformId() {
			return platformId;
		}

		public String getName() {
			return name;
		}

	}
}
